package com.yunshipei.ui.fragment;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.DelayEventModel;
import com.yunshipei.model.NotifyInfo;
import com.yunshipei.ui.activity.AlarmSetActivity;
import com.yunshipei.utils.DrawableUtils;

import io.rong.eventbus.EventBus;

//import de.greenrobot.event.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class DelayFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {
    private View view;
    private RadioGroup group;
    private NotifyInfo notifiInfo = null;
    private AlarmSetActivity alarmSetActivity = null;

    public DelayFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_delay, container, false);
        initView();
        return view;
    }

    public void initView() {
        group = (RadioGroup) view.findViewById(R.id.delay_id);
        int count = group.getChildCount();
        for (int i = 0; i < count; i++) {
            RadioButton rb = (RadioButton) group.getChildAt(i);
            int color = EnterConfig.getInstance().getGlobalColor();
            Drawable normalDraw = getActivity().getResources().getDrawable(R.drawable.select_off);
            Drawable single = getActivity().getResources().getDrawable(R.drawable.icon_checked_src);
            rb.setButtonDrawable(DrawableUtils.getYspCheckBoxDraw(color, normalDraw, single));
        }
        group.setOnCheckedChangeListener(this);
        alarmSetActivity = (AlarmSetActivity) getActivity();
        notifiInfo = alarmSetActivity.getNotifiInfo();
        if (notifiInfo != null) {
            int delayTime = Integer.parseInt(notifiInfo.getDelayTime());
            switch (delayTime) {
                case 5:
                    group.check(R.id.five_radio);
                    break;
                case 10:
                    group.check(R.id.ten_radio);
                    break;
                case 30:
                    group.check(R.id.thirty_radio);
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getCheckedRadioButtonId()) {
            case R.id.five_radio:
                sendEventInfo(5);
                break;
            case R.id.ten_radio:
                sendEventInfo(10);
                break;
            case R.id.thirty_radio:
                sendEventInfo(30);
                break;
            default:
                break;
        }
    }

    public void sendEventInfo(int delayTime) {
        DelayEventModel delayEventModel = new DelayEventModel();
        delayEventModel.setDelayTime(delayTime);
        EventBus.getDefault().post(delayEventModel);
    }
}
